package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.http.Request;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Comment;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.IgnoreAuth;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.CommentService;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/9/9 20:23
 * @DESCRIBE
 */
@StudentRoles
@Path(value = "/comment", restful = true)
public class CommentHandlerApi {

    @Inject
    private CommentService commentService;

    @Inject
    private CourseService courseService;

    @PostRoute("/publish")
    public RestResponse publish(Request request,
                                Comment comment,
                                @Param int kid,
                                @Param String content,
                                @Param Integer star,
                                @Param(defaultValue = "1") Integer page,
                                @Param(defaultValue = "1") Integer size) {
        User user = TokenUtils.currentUser();
        comment.setCourseId(kid);
        comment.setUser(user);
        comment.setUserId(user.getId());
        comment.setContent(content);
        comment.setStar(star);
        if (commentService.pages(user.getId(), kid, page, size) != null) {
            Integer id = commentService.pages(user.getId(), kid, page, size).getRows().get(0).getId();
            commentService.delete(id);
        }
        Comment add = commentService.add(comment);
        request.attribute("commented", true);
        if (add != null) {
            return RestResponse.ok(add);
        }
        return RestResponse.fail();
    }

    @IgnoreAuth
    @PostRoute("/page")
    public RestResponse page(@Param int kid,
                             @Param(defaultValue = "1") int page,
                             @Param(defaultValue = "8") int size) {
        Page<Comment> page1 = commentService.page(kid, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @PostRoute("/pages")
    public RestResponse pages(@Param Integer uid,
                              @Param(defaultValue = "1") int page,
                              @Param(defaultValue = "8") int size) {
        Page<Comment> page1 = commentService.pages(uid, null, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @PostRoute("/myComment")
    public RestResponse myComment(@Param Integer kid,
                                  @Param(defaultValue = "1") int page,
                                  @Param(defaultValue = "8") int size) {
        Integer uid = TokenUtils.currentUser().getId();
        Page<Comment> page1 = commentService.pages(uid, kid, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    /**
     * 评价删除 只有是该课程的发布者才能进行删除
     *
     * @param id       评价id
     * @param courseId 课程id
     */
    @PostRoute("/deleteById")
    public RestResponse deleteById(@Param Integer id,
                                   @Param(name = "kid") Integer courseId) {
        User user = TokenUtils.currentUser();
        boolean myPublish = courseService.isMyPublish(courseId, user.getId());
        if (!myPublish) return RestResponse.fail("无权删除！");

        boolean commentPage = commentService.delete(id);
        if (commentPage) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
